<template>
	<div class="card">
		<el-row>
			<el-col :span="10">
				<DetailsForm :info-form="userInfo" :is-register="false" title="用户基本信息" />
			</el-col>
			<el-col :span="12">
				<div class="content-box">
					<el-text size="large" style="margin: 20px 0" truncated>车辆基本信息</el-text>
					<el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="140px">
						<el-form-item label="预计到校时间" required>
							<el-form-item prop="proDate">
								<el-date-picker v-model="ruleForm.proDate" type="date" placeholder="请选择预计到校日期" style="width: 100%" />
							</el-form-item>
							<el-col class="text-center" :span="1">
								<span class="text-gray-500">-</span>
							</el-col>
							<el-form-item prop="proTime">
								<el-time-picker v-model="ruleForm.proTime" placeholder="请选择预计到校时间" style="width: 100%" />
							</el-form-item>
						</el-form-item>
						<el-row>
							<el-form-item label="已入校" prop="isEntrance">
								<el-switch v-model="ruleForm.isEntrance" />
							</el-form-item>
							<el-form-item label="停车区域" v-show="ruleForm.isEntrance" prop="region">
								<el-select v-model="ruleForm.region" placeholder="请选择停车区域">
									<el-option v-for="item in regionOptions" :key="item" :label="item" :value="item" />
								</el-select>
							</el-form-item>
						</el-row>
						<el-form-item label="车型" prop="resource">
							<el-radio-group v-model="ruleForm.resource">
								<el-radio label="Sponsorship" />
								<el-radio label="Venue" />
							</el-radio-group>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="submitForm(ruleFormRef)">Create</el-button>
							<el-button @click="resetForm(ruleFormRef)">Reset</el-button>
						</el-form-item>
					</el-form>
				</div>
			</el-col>
		</el-row>
	</div>
</template>

<script setup lang="ts" name="dynamicForm">
import DetailsForm from "@/components/DetailsForm/index.vue";
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { checkPhoneNumber } from "@/utils/eleValidate";
import { ElMessage } from "element-plus";

const userInfo = reactive({
	uid: "010371001",
	name: "用户",
	phone: "",
	mail: ""
});
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
	region: "",
	proDate: "",
	proTime: "",
	isEntrance: null,
	resource: ""
});

const rules = reactive<FormRules>({
	name: [
		{ required: true, message: "请输入用户名，用户名为用户姓名", trigger: "blur" },
		{ min: 2, max: 5, message: "用户名应包含2~5个字符", trigger: "blur" }
	],
	phone: [{ required: true, validator: checkPhoneNumber, trigger: "blur" }],
	region: [
		{
			message: "请选择车辆停放的区域",
			trigger: "change"
		}
	],
	proDate: [
		{
			type: "date",
			required: true,
			message: "Please pick a date",
			trigger: "change"
		}
	],
	proTime: [
		{
			type: "date",
			required: true,
			message: "Please pick a time",
			trigger: "change"
		}
	],
	resource: [
		{
			required: true,
			message: "Please select activity resource",
			trigger: "change"
		}
	]
});

const regionOptions = ["A", "B", "C", "D", "E"];

const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			ElMessage.success("提交的数据为 : " + JSON.stringify(ruleForm));
		} else {
			console.log("error submit!", fields);
		}
	});
};

const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
};
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
